package com.ygqh.baby.controller.mobile;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.po.YgFunnyVideo;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgFunnyVideoPraiseService;
import com.ygqh.baby.service.YgFunnyVideoService;
import com.ygqh.baby.service.YgUserService;

@Controller()
@RequestMapping("/mobile/funnyvideo")
public class YgFunnyVideoController extends BaseController {
	protected Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private YgFunnyVideoService ygFunnyVideoService;
	@Autowired
	private YgUserService ygUserService;
	@Autowired
	private YgFunnyVideoPraiseService ygFunnyVideoPraiseService;

	@RequestMapping(value = "/addFunnyVideo")
	@ResponseBody
	public JSONPObject addFunnyVideo(String title, String videoUrl, String imgPath, String remark, String callback) {
		// YgUser user = SessionUtil.getCurrentUser();
		Message message = ygFunnyVideoService.addFunnyVideo(title, videoUrl, imgPath, 1L, remark);
		return new JSONPObject(callback, message);

	}

	@RequestMapping(value = "/findById")
	@ResponseBody
	public JSONPObject findById(Long id, String callback) {
		// FYgUser user = SessionUtil.getCurrentUser();
		YgFunnyVideo message = ygFunnyVideoService.findById(id);
		return new JSONPObject(callback, Message.success(message));

	}

	@RequestMapping(value = "/findList")
	@ResponseBody
	public JSONPObject findList(QueryInfo queryInfo, Long userId, String callback) {
		// FYgUser user = SessionUtil.getCurrentUser();
		List<YgFunnyVideo> message = ygFunnyVideoService.findVideoList(queryInfo, userId);
		return new JSONPObject(callback, Message.success(message));

	}

	@RequestMapping(value = "/pointPraise")
	@ResponseBody
	public JSONPObject pointPraise(Long videoId, String userName, String openId, String nickName, String headUrl,
			String callback) {
		// FYgUser user = SessionUtil.getCurrentUser();
		if (!StringUtils.isEmpty(userName)) {
			YgUser user = ygUserService.findByUserName(userName);
			if (user == null) {
				return new JSONPObject(callback, Message.error("用户不存在", userName));
			}
			openId = userName;
			headUrl = user.getHeadImageUrl();
			nickName = user.getNickName();
		}
		Message message = ygFunnyVideoService.pointPraise(videoId, openId, nickName, headUrl);
		return new JSONPObject(callback, Message.success(message));

	}

	@RequestMapping(value = "/cancelPraise")
	@ResponseBody
	public JSONPObject cancelPraise(Long videoId, String userNameOrOpenId, String callback) {
		Message message = ygFunnyVideoService.cancelPraise(videoId, userNameOrOpenId);
		return new JSONPObject(callback, Message.success(message));

	}
}
